const BASE_URL = 'http://127.0.0.1:9001/api/wx'

const request = (option) => {
    const header = {
        'content-type': 'application/json'
    }
    const token = uni.getStorageSync('TOKEN')
    if (token) {
        header.Authorization = token;
    }
    return new Promise((resolve, reject) => {
        uni.request({
            url: BASE_URL + option.url,
            method: option.method || 'GET',
            header: header,
            data: option.data,
            success(resp) {
                if (resp.statusCode !== 200) {
                    return uni.showToast({title: "网络异常，请稍后再试！"})
                }
                const data = resp.data
                if (data.code === 444) {
                    uni.removeStorageSync("TOKEN")
                    uni.showToast({title: "登陆过期，请重新登陆"})
                } else if (data.code !== 200) {
                    let message = "网络异常，请稍后再试！"
                    if (data && data.message) {
                        message = data.message
                    }
                    return uni.showToast({title: message})
                }
                resolve(data)
            },
            fail(err) {
                uni.showToast({title: "网络异常，请稍后再试！"})
                reject(err)
            }
        })
    })
}

export default request